
package com.shycloud.mido.basic.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shycloud.mido.common.core.util.R;
import com.shycloud.mido.common.log.annotation.SysLog;
import com.shycloud.mido.basic.api.entity.BasSensitiveWords;
import com.shycloud.mido.basic.service.BasSensitiveWordsService;
import com.shycloud.mido.common.security.util.SecurityUtils;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;


/**
 * 敏感词表 前端控制器
 *
 * @author yyy
 * @date 2019-02-26 16:32:57
 */
@RestController
@AllArgsConstructor
@RequestMapping("/sensitiveWords")
@Api(value = "sensitiveWords", description = "敏感词表管理")
public class SensitiveWordsController {

  private final  BasSensitiveWordsService basSensitiveWordsService;

  /**
   * 分页查询
   * @param page 分页对象
   * @param basSensitiveWords 敏感词表
   * @return
   */
  @GetMapping("/page")
  public R getBasSensitiveWordsPage(Page page, BasSensitiveWords basSensitiveWords) {
  	page.setDesc("create_time");
    return  new R<>(basSensitiveWordsService.page(page,Wrappers.query(basSensitiveWords)));
  }


  /**
   * 通过id查询敏感词表
   * @param sensId id
   * @return R
   */
  @GetMapping("/{sensId}")
  public R getById(@PathVariable("sensId") Integer sensId){
    return new R<>(basSensitiveWordsService.getById(sensId));
  }

  /**
   * 新增敏感词表
   * @param basSensitiveWords 敏感词表
   * @return R-0
   */
  @SysLog("新增敏感词表")
  @PostMapping
  @PreAuthorize("@pms.hasPermission('bas_sensitiveWords_add')")
  public R save(@RequestBody BasSensitiveWords basSensitiveWords){
	  String username = SecurityUtils.getUser().getUsername();
	  int n = basSensitiveWordsService.selByText(basSensitiveWords.getText());
	  if(n > 0){
		  return new R<>(n);
	  }else{
		  basSensitiveWords.setCreateUser(username);
		  basSensitiveWordsService.saveWords(basSensitiveWords);
		  return new R<>(n);
	  }

  }

  /**
   * 修改敏感词表
   * @param basSensitiveWords 敏感词表
   * @return R
   */
  @SysLog("修改敏感词表")
  @PutMapping
  @PreAuthorize("@pms.hasPermission('bas_sensitiveWords_edit')")
  public R updateById(@RequestBody BasSensitiveWords basSensitiveWords){
  	LocalDateTime localDateTime = LocalDateTime.now();
  	basSensitiveWords.setUpdateTime(localDateTime);
  	String username = SecurityUtils.getUser().getUsername();
  	basSensitiveWords.setCreateUser(username);
    return new R<>(basSensitiveWordsService.updateById(basSensitiveWords));
  }

  /**
   * 通过id删除敏感词表
   * @param sensId id
   * @return R
   */
  @SysLog("删除敏感词表")
  @DeleteMapping("/{sensId}")
  @PreAuthorize("@pms.hasPermission('bas_sensitiveWords_del')")
  public R removeById(@PathVariable Integer sensId){
    return new R<>(basSensitiveWordsService.removeById(sensId));
  }

}
